program vanderwaal
	implicit none
	real :: x, root
	print *,'Enter a number to start with'; read *,x
	root = fs_newtonRhapsdon(x, 0.0001)
contains
	real function f(x) 
		real, intent(in)	::	x
		real				::	a = 3.592
		real				::	b = 0.04267
		real				::	p = 1
		real				::	RT = 24.6162	
		f = x - a/x+ a*b/(x**2) - b - RT
	end function f

	real function f1(x) 
		real, intent(in)	::	x
		real				::	a = 3.592
		real				::	b = 0.04267
		real				::	p = 1
		real				::	RT = 24.6162	
		f1=1 - a/(x**2) - 2*a*b/(x**3)
	end function f1
	
	recursive function fs_newtonRhapsdon(x1, convergence_limit) result(root)
		real, intent(in)	:: x1				!  x1 and x2 should bracket the root
	    real, intent(in)	:: convergence_limit	!  absolute limit for resolution of root eg.-use 0.00001
	    real             	:: root
	    real				:: x0
		x0=x1-f(x1)/f1(x1)
		
		print *,x0
		if( abs((x0-x1)/x1) < convergence_limit ) then 
			write(*,*) x0  
			root = x0 
			return
		else 
			root = fs_newtonRhapsdon(x0, convergence_limit) 
		end if 
	end function fs_newtonRhapsdon
end program